/***********************************************************************
    filename:   DaterGroup.h
    created:    6/5/2012
    author:     4Ys

    purpose:    Defines class for Group objects
*************************************************************************/
#ifndef _DaterGroup_h_
#define _DaterGroup_h_

#include "DaterUser.h"
#include "DaterGroupEventMap.h"
#include "DaterTime.h"
#include "DaterIteratorBase.h"

#include <map>

namespace Dater
{
	class Group
	{
	public:
		/*!
		\brief
			Constructor of the class.

		\param name
			- a string means the name of the group.
		*/
		Group(int gid) {
			_gid = gid;
		};

		/*!
		\brief
			Destructor of group object.
		*/
		~Group() { };

		/*!
		\brief
			return the gid of the group.

		\return
			the gid of the group.
		*/
		int getGid(){return _gid;};

		/*!
		\brief
			Set the name of the Group.

		\param gid
			the gid of the group.
		
		\return 
			Nothing.
		*/
		void setGid(int gid){_gid = gid; return;};

		/*!
		\brief
			return the name of the group.

		\return
			the name of the group.
		*/
		std::string getName(){return _name;};

		/*!
		\brief
			Set the name of the Group.

		\param name
			the name of the group.
		
		\return 
			Nothing.
		*/
		void setName(std::string name){_name = name; return;};

		std::string getType(){return _type;}

		void setType(std::string type){_type = type;}

		std::string getDescription(){return _description;}

		void setDescription(std::string description){_description = description;}

		/*!
		\brief
			return the CreatorID of the group.

		\return
			the CreatorID of the group.
		*/
		int getCreatorID(){return _creatorid;};

		/*!
		\brief
			Set the creatorid of the Group.

		\param gid
			the creatorid of the group.
		
		\return 
			Nothing.
		*/
		void setCreatorID(int creatorid){_creatorid = creatorid; return;};

		/*!
		\brief
			Add a user to the Group.

		\param uid
			the uid of the user.
		
		\return 
			Nothing.
		*/
		void addUser(User* user){
			if(_userList.find(user->getUid()) != _userList.end())
			{
				return;
			}else
			{
				_userList[user->getUid()] = user;
				return;
			}
		};
		
		/*
		\brief
			Add a event to the Group.

		\param 
		
		\return 
			Nothing.
		*/
		//void addEvent(GroupEvent* gEve){_eventList.push_back(gEve); return;};
		GroupEventMap* addGroupEventMap(int groupEventID, User* publisher, const std::string& name,
			const Time& startTime, const Time& finishTime,const Time& createTime, const std::string& description)
		{
			if(_groupEventMapList.find(groupEventID) != _groupEventMapList.end())
			{
				return _groupEventMapList[groupEventID];
			}
			else
			{
				GroupEventMap* newGroupEventMap = new GroupEventMap(_gid, groupEventID, publisher, name, startTime, finishTime, 
				createTime, description);
				_groupEventMapList[groupEventID] = newGroupEventMap;
				return newGroupEventMap;
			}
		};

		const std::map<int, User*>& getUserList()
		{
			return _userList;
		}

		const std::map<int, GroupEventMap*, std::greater<int>>& getGroupEventMapList()
		{
			return _groupEventMapList;
		}

		GroupEventMap* getGroupEventMap(int groupEventID)
		{
			if(_groupEventMapList.find(groupEventID) != _groupEventMapList.end())
				return _groupEventMapList[groupEventID];
			else
				return NULL;
		}

		GroupEventMap* getLatestGroupEventMap()
		{
			if(_groupEventMapList.size() == 0)
				return NULL;
			else
				return _groupEventMapList.begin()->second;
		}

		const Time& getCreateTime()
		{
			return _createTime;
		}

		void setCreateTime(const Time& createTime)
		{
			_createTime = createTime;
		}

		/*!
		\brief
			Delete a user from the Group.

		\param uid
			the uid of the user.
		
		\return 
			Nothing.
		
		void deleteUser(int uid);
		*/
		
		/*!
		\brief
			Delete a event from the Group.

		\param eid
			the eid of the event.
		
		\return 
			Nothing.
		
		void deleteEvent(int eid);
		*/

	private:
		//! The ID of the group.
		int _gid;

		//! A string implies name.
		std::string _name;

		std::string _type;

		std::string _description;

		//! The ID of creator.
		int _creatorid;

		//! A vector contains a list of users in group.
		std::map<int, User*> _userList;

		//! A vector contains a list of events in group.
		std::map<int, GroupEventMap*, std::greater<int>> _groupEventMapList;

		Time _createTime;
	};
}
#endif